package gold.digger;

import gold.utils.InputUtil;
import gold.vo.CodeDoc;
import gold.vo.TreeNode;

import java.util.Arrays;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC129 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        int totalSum = 0;

        public int sumNumbers(TreeNode root) {
            getSumRecur(root, 0);
            return totalSum;
        }

        public void getSumRecur(TreeNode root, int parentSum) {
            if (null == root) return;
            if (null == root.left && null == root.right) {
                totalSum += parentSum * 10 + root.val;
                return;
            }

            parentSum = parentSum * 10 + root.val;
            getSumRecur(root.left, parentSum);
            getSumRecur(root.right, parentSum);
        }
    }

    public void run() {
        Solution solution = new Solution();
        TreeNode root = TreeNode.createTree("[4,9,0,5,1]");
        System.out.println(solution.sumNumbers(root));
    }

    public static void main(String[] args) throws Exception {
        LC129 an = new LC129();
        an.run();

        Arrays.toString("".split(","));
        InputUtil.run();
        CodeDoc.run();
        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
